<?php
namespace app\admin\controller;
header("content-type:text/html;charset=utf-8");
use app\admin\model\Setting;
use app\admin\model\Text;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;
use think\Controller;
use think\Session;
use think\Request;


class Base extends Controller
{

    //初始化方法，相当于析构方法
    protected function _initialize(){
        header("content-type:text/html;charset=utf-8");
        header("Access-Control-Allow-Origin:*");
        header("Access-Control-Allow-Headers:Origin,X-Requested-With,Content-Type,Accept");
        header('Access-Control-Allow-Methods:GET,POST,PUT');
        ksort($_POST);
        ksort($_GET);
    }
     //判断用户是否存在
    public function user_info(){

        if(Session::has('user_info')){
            return Session::get("user_info");
        }else{
           echo "<script>location.href='/public/Admin/Login/Logins';</script>";
           exit();
        }
    }
    //获取setting
    public function getSetting($key){
        return Setting::where(array("k"=>$key))->find();
    }
    //获取所有的setting
    public function getAllSetting(){
        $ress = Setting::select();
        $ress = collection($ress)->toArray();
        $arr = array();
        foreach($ress as $k => $val){
            $arr[$ress[$k]['k']] = $val['val'];
        }
        return $arr;
    }
    //获取所有的text
    public function getAllText(){
        $ress = Text::select();
        $ress = collection($ress)->toArray();
        $arr = array();
        foreach($ress as $k => $val){
            $arr[$ress[$k]['k']] = $val['val'];
        }
        return $arr;
    }
    //输出测试
    public function preEcho($data){
        echo "<pre>";
        var_dump($data);
        echo "</pre>";
        exit();
    }
    //输出测试
    public function preEcho2($data){
        echo "<pre>";
        var_dump($data);
        echo "</pre>";
    }
  //获取指定文件夹中的文件
  public function get_allfiles($path,&$files) {
    if(is_dir($path)){
      $dp = dir($path);
      while ($file = $dp->read()){
        if($file !="." && $file !=".."){
          $this->get_allfiles($path."/".$file, $files);
        }
      }
      $dp ->close();
    }
    if(is_file($path)){
      $files[] = $path;
    }
  }
  //读取项目文件
  public function getmyfiles(Request $request){
    $img = array();
    $path =  ROOT_PATH .$request->post("path");
    $files = $this->get_allfiles($path ,$img);
    foreach($img as $key => $val){
      $img[$key]=str_replace($path."/","",$val);
    }
    $this->success($img);
  }


  //生成二维码
  public function qr_code($str){
    $writer = new PngWriter();  //实例写入二维码类
    $qrCode = QrCode::create($str); //创建二维码内容
    $result = $writer->write($qrCode); //写入二维码
    $url = str_replace("\\","/",ROOT_PATH)."public/static/admin/uploads/teacher_qrcode/"; //设置保存路径
    //如果保存路径不存在就创建
    if(!file_exists($url) && !is_dir($url)){
      mkdir($url);
    }
    $filename = time().".png"; //设置文件名
    $result->saveToFile($url.$filename); //把二维码保存起来
    return $filename; //返回二维码的地址
  }

  //视频生成二维码
  public function qr_code_dir($str,$dirname){
    $writer = new PngWriter();  //实例写入二维码类
    $qrCode = QrCode::create($str); //创建二维码内容
    $result = $writer->write($qrCode); //写入二维码
    $url = str_replace("\\","/",ROOT_PATH)."public/static/admin/uploads/{$dirname}/"; //设置保存路径
    //如果保存路径不存在就创建
    if(!file_exists($url) && !is_dir($url)){
      mkdir($url);
    }
    $filename = time().".png"; //设置文件名
    $result->saveToFile($url.$filename); //把二维码保存起来
    return $filename; //返回二维码的地址
  }



  //删除文件
  public function delfile(Request $request){
     $path =  $request->post();
//     var_dump($path);
     $file = str_replace("\\","/",ROOT_PATH).$path["path"];
     if(file_exists($file)){
       $res = unlink($file);
       if($res){
         $this->success("删除成功");
       }else{
         $this->success("删除失败");
       }
     }else{
       $this->success("该文件不存在！");

     }
  }


}
